package juc.base;

import java.util.concurrent.TimeUnit;

public class WaitDemo {
    /*守护线程*/
    public static void main(String[] args) throws Exception {
        Object lock = new Object();
        new Thread(() -> {
            System.out.println("wait start");
            synchronized (lock) {
                try {
                    /*wait会释放锁*/
                    lock.wait();
                    /*sleep，yeild不会释放锁*/
                    TimeUnit.SECONDS.sleep(6);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("wait end");
        }).start();

        TimeUnit.SECONDS.sleep(2);
        new Thread(() -> {
            synchronized (lock) {
                System.out.println("notify start");
                lock.notify();
                System.out.println("notify wait");
            }
        }).start();
    }
}